import { UploadMoreImg } from "../../../utils/function/tools";
import msg from "../../../utils/model/msg";

// pages/order/material/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    record: [], // 作为页面渲染
    user: {},
  },
  /**
   * @function 添加图片上传
   */
  add: function () {
    const user: any = this.data.user;
    chooseImg(user.id)
    .then((res) => {
      const record: any = this.data.record;
      record.push(res);
      this.setData({
        record: record,
      })
      msg.Success('上传成功');
    })
  },
  /**
   * @function 提交报名
   */
  submit: function () {
    const emitter = this.getOpenerEventChannel();
    wx.navigateBack();
    emitter.emit('submit', { img: JSON.stringify(this.data.record)});
  },
  /**
   * @function 取消
   */
  cancel: function () {
    wx.navigateBack();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const user = wx.getStorageSync('user');
    if (!user) {
      (async () => {
        await msg.Modal('登录失效');
        wx.navigateBack();
      })()
      return;
    }
    this.setData({
      user: user
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})

/**
 * @function 上传图片
 */
const uploadMoreImg = async (data: {
  tempFilePath: string, fileName: string, userid: string
}): Promise<{ status: number, msg: string, savename: string, allpath: string, }> => {
  return new Promise((resolve, reject) => {
    msg.Loading();
    UploadMoreImg({
      tempFilePath: data.tempFilePath,
      fileName: data.fileName,
      userid: data.userid
    })
      .then((res: any) => {
        msg.HideLoading();
        resolve(JSON.parse(res.data));
      })
      .catch(() => {
        msg.HideLoading();
        msg.Fail("上传失败");
        reject();
      })

  })

}
/**
 * @function 选择图片并命名
 */
const chooseImg = (userid: string): Promise<{ name: string, path: string }> => {
  return new Promise((resolve, reject) => {
    wx.chooseMedia({
      sourceType: ['album'],
      mediaType: ['image'],
      count: 1,
    })
      .then((res) => {
        const tempFilePath = res.tempFiles[0].tempFilePath;
        msg.Modal("", '图片命名', false, true)
          .then((res: any) => {
            const fileName = res.content;
            uploadMoreImg({
              tempFilePath: tempFilePath,
              fileName: fileName,
              userid: userid
            })
            .then((res) => {
              const name = res.savename;
              const path = 'https://sanli-tracks.com' + res.allpath;
              resolve({name, path});
            })
            .catch(() => { reject(); })
          })
          .catch(() => { reject(); })
      }).catch(() => { reject(); })
  })

}